const WXAPI = require('../../utils/apifm-ttapi')
const CONFIG = require('../../config.js')
const TOOLS = require('../../utils/tools.js')

//获取应用实例
var app = getApp()
Page({
  data: {
    bookList: [], // 书列表
    inputVal: "", // 搜索框内容

    inVogueList: [], // 爆款书
    newBookList: [], //新书推荐
    pingtuanList: [], //拼团商品列表
    banners:[],//轮播图
    hotBooks:[],//爆款推荐

    loadingHidden: false, // loading
    selectCurrent: 0,
    categories: [],
    activeCategoryId: 0,

    
    scrollTop: 0,
    loadingMoreHidden: true,

    coupons: [],

    curPage: 1,
    pageSize: 10,
    cateScrollTop: 0,
    test:[
      {id:'1',name:'test'}
    ]
  },

  // 爆款推荐详情
  toDetailsTap: function(e) {
    wx.navigateTo({
      url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
    })
  },
  // 轮播详情
  tapBanner: function(e) {
    if (e.currentTarget.dataset.id != 0) {
      wx.navigateTo({
        url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
      })
    }
  },
  // 未知
  bindTypeTap: function(e) {
    this.setData({
      selectCurrent: e.index
    })
  },
  // 回调函数
  onLoad: function(e) {
    wx.showShareMenu({
      withShareTicket: true
    }) 
    const that = this
    // if (e && e.query && e.query.inviter_id) { 
    //   wx.setStorageSync('referrer', e.query.inviter_id)
    // }
    if (e && e.scene) {
      const scene = decodeURIComponent(e.scene)
      if (scene) {        
        wx.setStorageSync('referrer', scene.substring(11))
      }
    }
    wx.setNavigationBarTitle({
      title: "白文故事" //wx.getStorageSync('mallName')
    })
    /**
     * 示例：
     * 调用接口封装方法
     */
    // 轮播图
    WXAPI.banners().then(function(res) {
      // var that = this
      if (res.code === 0) {
        that.setData({
          banners: res.data
        });
      } else {
        wx.showModal({
          title: '提示',
          content: '请在后台添加 banner 轮播图片，自定义类型填写 index',
          showCancel: false
        })
      }
    }).catch(function(e) {
      wx.showToast({
        title: res.msg,
        icon: 'none'
      })
    })
    //this.categories()
    // 我喜欢推荐
    WXAPI.getInVogue().then(res => {
      if (res.code === 0){
        that.setData({
          inVogueList: res.data
        })
      }      
    })
     // 爆款推荐
     WXAPI.getHotBooks().then(res => {
      if (res.code === 0){
        that.setData({
          hotBooks: res.data
        })
      }      
    })
    //that.getCoupons()
    that.getNotice();
    that.getNewBook();
    that.getBookList();
    // that.getHotBooks()
  },
  onShow: function(e){
    // 获取购物车数据，显示TabBarBadge
    TOOLS.showTabBarBadge();
  },
  // async categories(){
  //   const res = await WXAPI.goodsCategory()
  //   let categories = [];
  //   debugger;
  //   if (res.code == 0) {
  //     const _categories = res.data.filter(ele => {
  //       return ele.level == 1
  //     })
  //     categories = categories.concat(_categories)
  //   }
  //   this.setData({
  //     categories: categories,
  //     activeCategoryId: 0,
  //     curPage: 1
  //   });
  //   this.getGoodsList(0);
  // },
  // 滚动事件
  onPageScroll(e) {
    let scrollTop = this.data.scrollTop
    this.setData({
      scrollTop: e.scrollTop
    })
  },
  // 书刊
  async getBookList() {
    wx.showLoading({
      "mask": true
    })
    const res = await WXAPI.getBookList({
      page: this.data.curPage,
      pageSize: this.data.pageSize,
      type:'',
      name:""
    })
    wx.hideLoading()
    res.data.forEach(element => {
       if(!this.data.bookList.includes(element)){
        this.data.bookList.push(element);
       }
    });

    // if(this.data.bookList <= this.data.pageSize){
    //   books = res.data;
    // }else{
    //   books = this.data.bookList;
    // }
    
    // if(res.data > this.data.pageSize){
    //   for (var i = 0; i < res.data.length; i++) {
    //   books.push(res.data[i]);
    //   }
    // }
    // if(res.data < this.data.pageSize){
    //   this.setData({
    //     loadingMoreHidden: false
    //   });
    // }
    this.setData({
      bookList:  this.data.bookList
    });
  },

  // getCoupons: function() {
  //   var that = this;
  //   WXAPI.coupons().then(function (res) {
  //     debugger
  //     if (res.code == 0) {
  //       that.setData({
  //         coupons: res.data
  //       });
  //     }
  //   })
  // },
  //用户点击右上角转发
  onShareAppMessage: function() {    
    return {
      title: '"' + wx.getStorageSync('mallName') + '" ' + CONFIG.shareProfile,
      path: '/pages/index/index?inviter_id=' + wx.getStorageSync('uid')
    }
  },
  // 公告
  getNotice: function() {
    var that = this;
    WXAPI.noticeList().then(function (res) {
      if (res.code == 0) {
        that.setData({
          noticeList: res.data
        });
      }
    })
  },
  // 上拉出发
  onReachBottom: function() {
    let that = this;
    this.setData({
      curPage: that.data.curPage + 1
    });
    that.getBookList();
  },
  // 下拉触发
  onPullDownRefresh: function() {
    
    this.setData({
      curPage:1
    });
    this.getBookList()
    wx.stopPullDownRefresh()
  },
   // 新品书单
  async getNewBook(){
    const res = await WXAPI.getNewBook();
    if (res.code == 0) {
      this.setData({
        newBookList: res.data
      })
    }
  },
  // 优惠卷
  // goCoupons: function (e) {
  //   wx.navigateTo({
  //     url: "/pages/coupons/index"
  //   })
  // },
  // pingtuanGoods(){ // 获取团购商品列表
  //   const _this = this
  //   WXAPI.goods({
  //     pingtuan: true
  //   }).then(res => {
  //     if (res.code === 0) {
  //       _this.setData({
  //         pingtuanList: res.data
  //       })
  //     }
  //   })
  // },
  // 放值
  bindinput(e) {
    this.setData({
      inputVal: e.detail.value
    })
  },
  // 搜索
  bindconfirm(e) {
    this.setData({
      inputVal: e.detail.value
    })
    wx.navigateTo({
      url: '/pages/goods/list?name=' + this.data.inputVal,
    })
  },
  // 图书详情页
  toBookDetails(e){
    console.log(e,3333);
    wx.navigateTo({
      url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
    })
  }
})
